{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# C++的常量"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1. 常量"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "① 常量，用于记录程序中不可更改的数据。\n",
    "\n",
    "② C++定义常量两种方式，如下所示：\n",
    "\n",
    "1. #define 宏常量：#define 常量名 常量值  \n",
    " - note：通常在文件上方定义，表示一个常量。\n",
    "2. const修饰的变量 const 数据类型 常量名 = 常量值  \n",
    " - note：通常在变量定义前加关键字const，修饰该变量为常量，不可修改。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.1 #define 常量名 常量值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#include <iostream>\n",
    "using namespace std;\n",
    "\n",
    "#define Day 7 \n",
    "\n",
    "int main()\n",
    "{\n",
    "\n",
    "    /*\n",
    "    Day = 14;  //错误，Day是常量，一旦修改就会报错\n",
    "    cout << \"一周总共有：\" << Day << \"天\" << endl;\n",
    "    */\n",
    "\n",
    "    cout << \"一周总共有：\" << Day << \"天\" << endl;\n",
    "\n",
    "    // 输出语句：cout << \"提示语\" << 变量 << endl; \n",
    "\n",
    "    system(\"pause\");\n",
    "\n",
    "    return 0;\n",
    "\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "运行结果：  \n",
    " - 一周总共有：7天  \n",
    " - 请按任意键继续. . ."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.2 const 数据类型 常量名 = 常量值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#include <iostream>\n",
    "using namespace std;\n",
    "\n",
    "int main()\n",
    "{\n",
    "    /*\n",
    "    const int month = 12;\n",
    "    month = 24;  //错误，const修饰的变量也称为常量\n",
    "    */\n",
    "\n",
    "    int month = 12;\n",
    "    month = 24;  //正确，这样可以修改变量的值\n",
    "\n",
    "    cout << \"一年总共有：\" << month << \"个月\" << endl;\n",
    "\n",
    "    system(\"pause\");\n",
    "\n",
    "    return 0;\n",
    "\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "运行结果：  \n",
    " - 一年总共有：24个月  \n",
    " - 请按任意键继续. . ."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.6.3",
   "language": "python",
   "name": "python3.6.3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
